<?php
/**
 * MVM_MALL 网上商店系统
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * 
 * @author :     www.mvmmall.com <admin@mvmmall.com> 
 * @version :    v4.X
---------------------------------------------
 */
if(!defined('MVMMALL')){
    exit('Access Denied');
}
//插件的代码必须和文件名保持一致
$payment['advance']['name']    = '预存款支付';
//描述
$payment['advance']['desc']    = '会员预存款支付';
//支付费用
$payment['advance']['pay_fee'] = '0%';
// 是否支持在线支付
$spayment['advance']['online']  = 0;
//否支持货到付款
$payment['advance']['cod']     = 0;
//申请地址
$payment['advance']['reg']     = '0';
//版权信息
$payment['advance']['license']  = '版权所有www.mvmmall.com';
//接口需要的参数
$payment['advance']['cfg'] ='';
    
class advance
{
    var $cfg;
    
    function  advance($cfg = array())
    {
        foreach ($cfg AS $key=>$val)
        {
            $this->cfg[$val['name']] = $val['value'];
        }
    }
    
/*提交支付请求*/
function pay_send($sn,$amount)
{
    $lang = $GLOBALS[lang];
    if($GLOBALS['m_check_id']==null) {
        echo "<script language='javascript'>alert('$lang[login_please]');</script>";
        echo '<meta http-equiv="refresh" content="0;url=logging.php?action=login&referer=cart.php?action=list"/>';
        exit;
    }
    $m_now_time=time();
    $v_url = MVMMALL_DIR."/respond.php?code=".basename(__FILE__, '.class.php');;		///支付结果返回地址
    $result="
				<FORM  name=re METHOD=post ACTION='$v_url' target='_blank'>
				<input type='hidden' name='sn' value='$sn'>
				<input type='hidden' name='amount' value='$amount'>
				<input type='submit' value='$lang[advance_pay]'>
				</FORM>
				";
    $result=trim($result);
    return   $result;
}
    /**
     * 提交返回处理*
     */
    function pay_receive()
    {
        $sn     = strip_tags(trim($_REQUEST['sn']));//定单号
        $amount = trim($_REQUEST['amount']);//金额
        $lang   = $GLOBALS['lang'];
    if($GLOBALS['m_check_id']==null) {
        echo "<script language='javascript'>alert('$lang[login_please]');</script>";
        echo '<meta http-equiv="refresh" content="0;url=logging.php?action=login&referer=cart.php?action=list"/>';
        exit;
    }
    $m_now_time=time();
    $result = $GLOBALS['db']->query("SELECT member_money,member_id FROM `{$GLOBALS['tablepre']}member_table` WHERE member_id = '$GLOBALS[m_check_id]'");
    $list=$GLOBALS['db']->fetch_array($result);
    if ($list['member_money']<$amount){
      show_msg('money_error','member.php?action=money'); 
    }
     $order_list = order_info($sn);
     if ($order_list['order_amount']==floatval($amount)){
         /* 改变订单状态*/
         change_order($sn);
         //写入预存款记录
         $query = "INSERT INTO `{$GLOBALS['tablepre']}money_table` SET
                      money_sess     = '$sn',
                      money_id       = '$GLOBALS[m_check_id]',
                      money_add      = '-$amount',
                      money_reason   = '$lang[advance_pay]：$sn ',
                      modify_ip      = '$m_user_ip',
                      approval_date  = '$m_now_time',
                      register_date  = '$m_now_time'";
         $GLOBALS['db']->query($query);
         //更新会员预存款内容信息
         $GLOBALS[db]->query("UPDATE `{$GLOBALS['tablepre']}member_table` SET  member_money = member_money - '$amount' WHERE member_id = '$GLOBALS[m_check_id]'");
         //更新session状态
         $_SESSION['mvm_sess_money']=($list['member_money']-$amount);
         return true;
     }else {
         //和定单数据库不一致
         return false;
     }
    }
}